﻿using Autofac;
using Orchard.Environment.Extensions;
using Orchard.FileSystems.Dependencies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;

namespace Orchard.WebApi
{
    public class OrchardAssembliesResolver :DefaultAssembliesResolver,IAssembliesResolver
    {
        readonly ILifetimeScope _container;

        public OrchardAssembliesResolver(ILifetimeScope container) 
        {
            _container = container;
        }
        public ICollection<System.Reflection.Assembly> GetAssemblies()
        {
            //ICollection<Assembly> baseAssemblies = base.GetAssemblies();
            //List<Assembly> assemblies = new List<Assembly>(baseAssemblies);
            List<Assembly> assemblies = new List<Assembly>();
            var assemblyLoader = _container.Resolve<IAssemblyProbingFolder>();
            var extensionManager=_container.Resolve<IExtensionManager>();
            var assemblyFeatures = extensionManager.AvailableFeatures();
            var mappings = new Dictionary<string, HttpControllerDescriptor>();
            foreach (var assemblyFeature in assemblyFeatures)
            {
                var loadassembly = assemblyLoader.LoadAssembly(assemblyFeature.Name);
                if (null != loadassembly)
                {
                    assemblies.Add(loadassembly);
                }
            }
            return assemblies;
        }
    }
}
